﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using BCCommon;
using BCDto.Common.FAQ;
using BCService.Common.FAQ;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace BCTMSRESTful.Controllers.Management.Common.FAQ
{
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class FaqsController : ManagementBaseController
    {
        private readonly IFaqService faqService;

        public FaqsController(IFaqService faqService)
        {
            this.faqService = faqService;
        }

        /// <summary>
        /// 管理员查询常见问题列表（分页）
        /// </summary>
        /// <param name="faqId">常见问题ID</param>
        /// <param name="title">常见问题标题</param>
        /// <param name="keyWord">关键字</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页数据条数</param>
        /// <response code="200">管理员查询常见问题列表</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<FAQDto>), 200)]
        public IActionResult QueryFAQ(
            [FromQuery(Name = "faq_id")]long? faqId,
            [FromQuery(Name = "title")]string title,
            [FromQuery(Name = "key_word")]string keyWord,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "index")]int pageIndex = 1,
            [FromQuery(Name = "size")]int pageSize = 10
            )
        {
            return this.Ok(this.faqService.QueryFAQ(faqId, title, keyWord, start, end, false, true, pageIndex, pageSize));
        }

        /// <summary>
        /// 管理员回复
        /// </summary>
        /// <param name="faqId">常见问题ID</param>
        /// <param name="faqDto">常见问题模型</param>
        /// <response code="201">管理员回复</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpPut("{faq_id}")]
        [Log("管理员回复FAQ")]
        [ProducesResponseType(typeof(FAQDto), 200)]
        public IActionResult Put(
            [FromRoute(Name = "faq_id")]long faqId,
            [FromBody]FAQDto faqDto
            )
        {
            return this.Ok(this.faqService.ModifyReply(faqId, faqDto.Answer, faqDto.KeyWord, this.Admin.AdminId, this.Admin.FullName));
        }

        /// <summary>
        /// 修改是否显示
        /// </summary>
        /// <param name="faqId">常见问题ID</param>
        /// <param name="faqDto">常见问题模型</param>
        /// <response code="201">修改是否显示</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpPut("[action]/{faq_id}")]
        [Log("修改是否显示FAQ")]
        [ProducesResponseType(typeof(FAQDto), 200)]
        public IActionResult ModifyIsShow(
            [FromRoute(Name = "faq_id")]long faqId,
            [FromBody]FAQDto faqDto
            )
        {
            return this.Ok(this.faqService.ModifyIsShow(faqId, faqDto.IsShow));
        }

        /// <summary>
        /// 删除常见问题
        /// </summary>
        /// <param name="faqId">常见问题ID</param>
        /// <response code="204">删除常见问题</response>
        /// <returns></returns>
        [HttpDelete("{faq_id}")]
        [Log("删除FAQ")]
        [ProducesResponseType(typeof(FAQDto), 200)]
        public IActionResult Delete(
            [FromRoute(Name = "faq_id")]long faqId
            )
        {
            return this.Ok(this.faqService.ModifyIsDelete(faqId));
        }

        /// <summary>
        /// 查询特定数量常见问题
        /// </summary>
        /// <param name="index">起始下标</param>
        /// <param name="amount">数量</param>
        /// <response code="200">查询特定数量常见问题。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<FAQDto>), 200)]
        public IActionResult QuerySpecify([FromQuery(Name = "index")]int index, [FromQuery(Name = "amount")]int amount)
        {
            return this.Ok(this.faqService.QuerySpecify(index, amount));
        }

        #region 启用/禁用常见问题ID
        /// <summary>
        ///  启用/禁用常见问题ID
        /// </summary>
        /// <param name="faqId">常见问题ID</param>
        /// <param name="enabled">是否启用</param>
        /// <response code="204">启用/禁用常见问题ID成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("[action]/{faq_id}/{enabled}")]
        [Log("启用/禁用常见问题ID")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Enabled([FromRoute(Name = "faq_id")]long faqId,
            [FromRoute(Name = "enabled")]bool enabled)
        {
            if (enabled)
            {
                this.faqService.Enabled(faqId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
            else
            {
                this.faqService.Disabled(faqId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
        }
        #endregion
    }
}
